﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor;

namespace PRT {

    [CustomEditor(typeof(IBLProbe))]
    public class IBLProbeEditor : Editor {

        Camera CreateCaptureCamera() {
            Camera ret = null;
            GameObject target = GameObject.Find("IBL_Camera");
            if (target != null) {
                ret = target.GetComponent<Camera>();
            }
            if (ret != null)
                return ret;
            target = new GameObject("IBL_Camera", typeof(Camera));
            ret = target.GetComponent<Camera>();
            ret.clearFlags = CameraClearFlags.SolidColor;
            ret.backgroundColor = Color.clear;
            ret = target.GetComponent<Camera>();
            return ret;
        }

        public override void OnInspectorGUI() {
            this.DrawDefaultInspector();

            var target = this.target as IBLProbe;

            if (GUILayout.Button("生成Irr CubeMap")) {
                Camera cam = CreateCaptureCamera();
                try {
                    target.GenerateIrrCubeMap(cam);
                } finally {
                    if (cam != null) {
                        if (EditorApplication.isPlaying)
                            Destroy(cam.gameObject);
                        else
                            DestroyImmediate(cam.gameObject);
                    }
                }
            }
        }
    }

}
